file(GLOB SRC_LIST    CONFIGURE_DEPENDS "*.cpp")
file(GLOB HEADERS     CONFIGURE_DEPENDS "*.h" "*.hpp")
file(GLOB_RECURSE APP_IMAGES  RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" CONFIGURE_DEPENDS "assets/images/*.png")
file(GLOB_RECURSE APP_FONTS   RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" CONFIGURE_DEPENDS "assets/fonts/*.ttf")

# message(STATUS "APP_IMAGES count = ${APP_IMAGES}")
# message(STATUS "APP_FONTS  count = ${APP_FONTS}")

set(MODULE_NAME guiqml)
qt_add_library(${MODULE_NAME} SHARED
    ${SRC_LIST}
    ${HEADERS}
)
target_link_libraries(${MODULE_NAME} PRIVATE
    Qt6::Quick Qt6::Qml Qt6::QuickControls2 Qt6::Multimedia Qt6::Widgets
    ${OpenCV_LIBS}
)
target_compile_definitions(${MODULE_NAME} PRIVATE GUIQML_LIBRARY)
qt_add_qml_module(${MODULE_NAME}
    URI App
    VERSION 1.0
    QML_FILES
        qml/App.qml
        qml/Main.qml
    OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/App"
)
qt_add_resources(${MODULE_NAME} "app_assets"
    PREFIX "/assets"
    BASE   "${CMAKE_CURRENT_SOURCE_DIR}/assets"
    FILES
        ${APP_IMAGES}
        ${APP_FONTS}
)

if (WIN32)
    if (DEFINED Qt6_DIR)
        get_filename_component(QT_BIN_DIR "${Qt6_DIR}/../../../bin" ABSOLUTE)
        set(DEPLOYQT_COMMAND "${QT_BIN_DIR}/windeployqt")
    else ()
        find_program(DEPLOYQT_COMMAND windeployqt)
    endif ()
else ()
    find_program(DEPLOYQT_COMMAND linuxdeployqt)
endif ()

if (NOT DEPLOYQT_COMMAND)
    message(WARNING "win/linux deployqt not found. Please set Qt6_DIR to the Qt installation directory.")
else ()
    add_custom_command(TARGET ${MODULE_NAME} POST_BUILD
            COMMAND ${DEPLOYQT_COMMAND} $<TARGET_FILE:${MODULE_NAME}> --qmlimport ${CMAKE_CURRENT_SOURCE_DIR}/qml --qmldir ${CMAKE_CURRENT_SOURCE_DIR}/qml
            WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
            COMMENT "Running deployqt to handle dependencies..."
    )
endif ()


